// WndSystemMenu.cpp : implementation file
//

#include "stdafx.h"
#include "BBViewer.h"
#include "WndSystemMenu.h"
#include "UIConst.h"


// CWndSystemMenu

IMPLEMENT_DYNAMIC(CWndSystemMenu, CWnd)

CWndSystemMenu::CWndSystemMenu()
{
}

CWndSystemMenu::~CWndSystemMenu()
{
}


BEGIN_MESSAGE_MAP(CWndSystemMenu, CWnd)
	ON_MESSAGE(WM_CTL_MESSAGE, OnCtlMessage)
	ON_WM_CREATE()
	ON_WM_PAINT()
	ON_WM_SIZE()
END_MESSAGE_MAP()



// CWndSystemMenu message handlers


int CWndSystemMenu::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	int i;
	LPST_BMP_INFO lpBmpInfo;
	for (i=0;i<4;i++)
	{
		lpBmpInfo = theApp.BmpInfo.GetAt(biExit_normal+i);
		m_btnExit.SetBitmaps(i, lpBmpInfo->hbmpImage);
		lpBmpInfo = theApp.BmpInfo.GetAt(biMax_normal+i);
		m_btnMax.SetBitmaps(i, lpBmpInfo->hbmpImage);
		lpBmpInfo = theApp.BmpInfo.GetAt(biRestoreFM_normal+i);
		m_btnNor1.SetBitmaps(i, lpBmpInfo->hbmpImage);
		lpBmpInfo = theApp.BmpInfo.GetAt(biMin_normal+i);
		m_btnHide.SetBitmaps(i, lpBmpInfo->hbmpImage);
		lpBmpInfo = theApp.BmpInfo.GetAt(biFull_normal+i);
		m_btnFull.SetBitmaps(i, lpBmpInfo->hbmpImage);
		lpBmpInfo = theApp.BmpInfo.GetAt(biRestoreFF_normal+i);
		m_btnNor2.SetBitmaps(i, lpBmpInfo->hbmpImage);
	}

	int nButtonCount = 0;
	CCtrlButton *pBtn[6];
	pBtn[nButtonCount++] = &m_btnExit;
	pBtn[nButtonCount++] = &m_btnMax;
	pBtn[nButtonCount++] = &m_btnNor1;
	pBtn[nButtonCount++] = &m_btnHide;
	pBtn[nButtonCount++] = &m_btnFull;
	pBtn[nButtonCount++] = &m_btnNor2;

	for (i=0;i<nButtonCount;i++)
	{
		lpBmpInfo = theApp.BmpInfo.GetAt(biExit_normal+4*i);
		if (!pBtn[i]->Create(NULL, NULL, WS_CHILD|WS_VISIBLE, CRect(0, 0, 0, 0), this, lpBmpInfo->uImageID, NULL))
			return -1;
	}

	LoadText();

	m_ToolTipCtrl.Create(this);
	m_ToolTipCtrl.Activate(TRUE);

	m_ToolTipCtrl.AddTool(&m_btnExit, m_sarrText.GetAt(0));
	m_ToolTipCtrl.AddTool(&m_btnHide, m_sarrText.GetAt(1));
	m_ToolTipCtrl.AddTool(&m_btnMax, m_sarrText.GetAt(2));
	m_ToolTipCtrl.AddTool(&m_btnFull, m_sarrText.GetAt(3));
	m_ToolTipCtrl.AddTool(&m_btnNor1, m_sarrText.GetAt(4));
	m_ToolTipCtrl.AddTool(&m_btnNor2, m_sarrText.GetAt(4));

	m_btnExit.ShowWindow(SW_SHOW);
	m_btnMax.ShowWindow(SW_SHOW);
	m_btnHide.ShowWindow(SW_SHOW);
	m_btnFull.ShowWindow(SW_SHOW);
	m_btnNor1.ShowWindow(SW_HIDE);
	m_btnNor2.ShowWindow(SW_HIDE);

	return 0;
}

void CWndSystemMenu::OnPaint() 
{
	CPaintDC dc(this); // device context for painting

	CDC dcMemory;
	dcMemory.CreateCompatibleDC(&dc);
	CBitmap *pBmp, *pBmpOld;
	LPST_BMP_INFO lpBmpInfo;
	lpBmpInfo = theApp.BmpInfo.GetAt(biSystemMenuPanel);
	pBmp = CBitmap::FromHandle(lpBmpInfo->hbmpImage);
	pBmpOld = dcMemory.SelectObject(pBmp);
	dc.BitBlt(0, 0, lpBmpInfo->rtImageRect.Width(), lpBmpInfo->rtImageRect.Height(), &dcMemory, 0, 0, SRCCOPY);
	dcMemory.SelectObject(pBmpOld);

	dcMemory.DeleteDC();		//2013.04.29
	// Do not call CWnd::OnPaint() for painting messages

}

void CWndSystemMenu::OnSize(UINT nType, int cx, int cy) 
{
	CWnd::OnSize(nType, cx, cy);

	CRect rect;
	rect.top = 0;
	rect.bottom = CY_SYSTEMMENU_BTN;

	rect.left = 0;
	rect.right = CX_SYSTEMMENU_BTN;
	m_btnNor2.MoveWindow(&rect);
	m_btnFull.MoveWindow(&rect);

	rect.left += CX_SYSTEMMENU_BTN;
	rect.right += CX_SYSTEMMENU_BTN;
	m_btnHide.MoveWindow(&rect);

	rect.left += CX_SYSTEMMENU_BTN;
	rect.right += CX_SYSTEMMENU_BTN;
	m_btnNor1.MoveWindow(&rect);
	m_btnMax.MoveWindow(&rect);

	rect.left += CX_SYSTEMMENU_BTN;
	rect.right += CX_SYSTEMMENU_BTN;
	m_btnExit.MoveWindow(&rect);


}

BOOL CWndSystemMenu::PreTranslateMessage(MSG* pMsg)
{
	m_ToolTipCtrl.RelayEvent(pMsg);
	return CWnd::PreTranslateMessage(pMsg);
}

long CWndSystemMenu::OnCtlMessage(UINT wParam, LONG lParam)
{
	CWnd* pParent = GetParent();
	pParent->PostMessage(WM_CTL_MESSAGE, wParam, lParam);
	return 0;
}

void CWndSystemMenu::UpdateWnd()
{

}

void CWndSystemMenu::SetCheckMaximize(BOOL bCheck)
{
	if (bCheck)
	{
		m_btnMax.ShowWindow(SW_HIDE);
		m_btnNor1.ShowWindow(SW_SHOW);
	}
	else
	{
		m_btnNor1.ShowWindow(SW_HIDE);
		m_btnMax.ShowWindow(SW_SHOW);
	}
}

void CWndSystemMenu::SetCheckFull(BOOL bCheck)
{
	if (bCheck)
	{
		m_btnMax.EnableWindow(FALSE);
		m_btnNor1.EnableWindow(FALSE);

		m_btnFull.ShowWindow(SW_HIDE);
		m_btnNor2.ShowWindow(SW_SHOW);
	}
	else
	{
		m_btnMax.EnableWindow(TRUE);
		m_btnNor1.EnableWindow(TRUE);

		m_btnFull.ShowWindow(SW_SHOW);
		m_btnNor2.ShowWindow(SW_HIDE);
	}
}

void CWndSystemMenu::RefreshLang()
{
	LoadText();

	m_ToolTipCtrl.DelTool(&m_btnExit);
	m_ToolTipCtrl.DelTool(&m_btnHide);
	m_ToolTipCtrl.DelTool(&m_btnMax);
	m_ToolTipCtrl.DelTool(&m_btnFull);
	m_ToolTipCtrl.DelTool(&m_btnNor1);
	m_ToolTipCtrl.DelTool(&m_btnNor2);

	m_ToolTipCtrl.AddTool(&m_btnExit, m_sarrText.GetAt(0));
	m_ToolTipCtrl.AddTool(&m_btnHide, m_sarrText.GetAt(1));
	m_ToolTipCtrl.AddTool(&m_btnMax, m_sarrText.GetAt(2));
	m_ToolTipCtrl.AddTool(&m_btnFull, m_sarrText.GetAt(3));
	m_ToolTipCtrl.AddTool(&m_btnNor1, m_sarrText.GetAt(4));
	m_ToolTipCtrl.AddTool(&m_btnNor2, m_sarrText.GetAt(4));

}

void CWndSystemMenu::LoadText()
{
	m_sarrText.RemoveAll();

	m_sarrText.Add(theApp.GetUIString(APPNAME_BUTTON, KEYNAME_BUTTON_EXIT, KEYNAME_BUTTON_EXIT));
	m_sarrText.Add(theApp.GetUIString(APPNAME_BUTTON, KEYNAME_BUTTON_HIDE, KEYNAME_BUTTON_HIDE));
	m_sarrText.Add(theApp.GetUIString(APPNAME_BUTTON, KEYNAME_BUTTON_MAX, KEYNAME_BUTTON_MAX));
	m_sarrText.Add(theApp.GetUIString(APPNAME_BUTTON, KEYNAME_BUTTON_FULL, KEYNAME_BUTTON_FULL));
	m_sarrText.Add(theApp.GetUIString(APPNAME_BUTTON, KEYNAME_BUTTON_RESTORE, KEYNAME_BUTTON_RESTORE));
}